package cn.lbz.Food.Edit;

import cn.lbz.Food.Init.DBUtils;
import cn.lbz.Food.Init.DBfood;
import cn.lbz.Food.Init.Page;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serial;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

public class SearchFoodServlet extends HttpServlet {

    @Serial
    private static final long serialVersionUID = 1198763442711986380L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String key = request.getParameter("name");

        String sql;
        if (key != null && !key.isEmpty()) {
            sql = "SELECT * FROM `share_food` WHERE" +
                    " `name` LIKE '%" + key + "%'" +
                    " OR `reason` LIKE '%" + key + "%'" +
                    " OR `located` = '" + key + "'";
        } else {
            sql = "SELECT * FROM `share_food` ORDER BY `id` DESC LIMIT 6";
        }

        try {
            List<DBfood> books = DBUtils.getfood(sql);
            try (Writer writer = response.getWriter()) {

                String sb = Page.getHeader() +
                        "<center><form action='./searchfood' method='GET'>" +
                        "<input type='text' name='name'/>&nbsp;&nbsp;&nbsp;" +
                        "<input type='submit' value='查询'/>" +
                        "</form></center>" +
                        toHtmlTable(books) +
                        Page.getEND();
                writer.write(sb);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private String toHtmlTable(List<DBfood> books) {
        StringBuilder sb = new StringBuilder();
        sb.append("<center><br><table style='width:70%' border='0' cellpadding='10'>");

        sb.append("<tr style='background-color:#1d1b16;color:#cda45e'>" +
                "<th>编号</th><th>食物名称</th><th>地点</th><th>类型</th><th>价格</th><th>推荐原因</th>" +
                "</tr>");
        for (int i=0; i<books.size(); i++) {
            DBfood book = books.get(i);
            if (i % 2 == 0) {
                sb.append("<tr style='background-color:#000000'>");
            } else {
                sb.append("<tr style='background-color:#1d1b16'>");
            }
            sb.append("<td>").append(book.getId()).append("</td>");
            sb.append("<td><b>").append(book.getName()).append("</b></td>");
            sb.append("<td>").append(book.getLocated()).append("</td>");
            sb.append("<td>").append(book.getType()).append("</td>");
            sb.append("<td>").append(book.getPrice()).append("</td>");
            sb.append("<td>").append(book.getreason()).append("</td>");
            sb.append("</tr>");
        }
        sb.append("</table></center>");
        return sb.toString();
    }
}
